package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysBookVo;
import org.dromara.system.domain.bo.SysBookBo;
import org.dromara.system.service.ISysBookService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 图书
 * 前端访问路由地址为:/system/book
 *
 * @author guoao
 * @date 2024-04-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/book")
public class SysBookController extends BaseController {

    private final ISysBookService sysBookService;

    /**
     * 查询图书列表
     */
    @SaCheckPermission("system:book:list")
    @GetMapping("/list")
    public TableDataInfo<SysBookVo> list(SysBookBo bo, PageQuery pageQuery) {
        return sysBookService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出图书列表
     */
    @SaCheckPermission("system:book:export")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysBookBo bo, HttpServletResponse response) {
        List<SysBookVo> list = sysBookService.queryList(bo);
        ExcelUtil.exportExcel(list, "图书", SysBookVo.class, response);
    }

    /**
     * 获取图书详细信息
     *
     * @param bookId 主键
     */
    @SaCheckPermission("system:book:query")
    @GetMapping("/{bookId}")
    public R<SysBookVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bookId) {
        return R.ok(sysBookService.queryById(bookId));
    }

    /**
     * 新增图书
     */
    @SaCheckPermission("system:book:add")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysBookBo bo) {
        return toAjax(sysBookService.insertByBo(bo));
    }

    /**
     * 修改图书
     */
    @SaCheckPermission("system:book:edit")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysBookBo bo) {
        return toAjax(sysBookService.updateByBo(bo));
    }

    /**
     * 删除图书
     *
     * @param bookIds 主键串
     */
    @SaCheckPermission("system:book:remove")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bookIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bookIds) {
        return toAjax(sysBookService.deleteWithValidByIds(List.of(bookIds), true));
    }
}
